package com.aramis.mydytt.ui.search.adapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.aramis.mydytt.R
import com.aramis.mydytt.ui.bean.MovieField

/**
 * Description: SearchTagAdapter
 * @author lizhidan
 * Created: 2021/5/6
 */
class SearchTagAdapter(style: Int) : RecyclerView.Adapter<SearchTagAdapter.ViewHolder>() {
    private val dataList = MovieField.getFiledList(style) ?: mutableListOf()
    private var selectPosition = 0
    var onItemClickListener: (() -> Unit)? = null

    fun getSelectField(): MovieField {
        return if (dataList.isNotEmpty()) dataList[selectPosition] else MovieField.emptyField()
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val view = LayoutInflater.from(parent.context).inflate(R.layout.item_search_tag, parent, false)
        return ViewHolder(view)
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        holder.tagTextView.text = dataList[position].fieldName
        holder.tagTextView.setOnClickListener {
            selectPosition = holder.layoutPosition
            notifyDataSetChanged()
            onItemClickListener?.invoke()
        }
        holder.tagTextView.setBackgroundResource(if (selectPosition == position) R.drawable.bg_search_tag_selected else 0)
    }

    override fun getItemCount(): Int = dataList.size

    class ViewHolder(val view: View) : RecyclerView.ViewHolder(view) {
        val tagTextView: TextView = view.findViewById(R.id.text_search_tag)
    }

}